package com.clip.service;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.dao.ClipeventPicDAO;
import com.clip.entity.ClipeventPic;
import com.clip.util.ScaleImage;
import com.clip.util.ToolsUtils;

@Service
@Transactional
public class ClipeventPicService {
	@Autowired
	private ClipeventPicDAO clipeventPicDAO;
	
	private final byte[] buffer = new byte[1024*8];
	private ScaleImage scaleImage = new ScaleImage();
	
	private int IMGMAXWIDTH=600;
	private int SMALLIMGMAXHEIGHT=120;
	private int SMALLIMGMAXWIDTH=120;
//	private int IMGMAXHEIGHT=600;
	public ClipeventPic saveTempClippic(int uid,File file,String filename){
		Calendar now=Calendar.getInstance();
		String webPath=null;
		String smallWebPath=null;
		if(file!=null){
			try {
				webPath=ToolsUtils.getStringForMD5(""+uid+now.getTimeInMillis())+filename.substring(filename.lastIndexOf(".")).toLowerCase();
				smallWebPath="S_"+ToolsUtils.getStringForMD5(""+uid+now.getTimeInMillis())+filename.substring(filename.lastIndexOf(".")).toLowerCase();
				String dPath=ToolsUtils.getApachePhysicalURL().get("apacheClippicPath")+"/";
				String savePath=ToolsUtils.getApachePhysicalURL().get("apacheClippicPath")+"/"+webPath;
				if(!(new File(dPath).exists())){
					new File(dPath).mkdirs();
				}
				int width,height;
				BufferedInputStream bis = null;
				BufferedOutputStream bos = null;
				BufferedImage img = ImageIO.read(file);
				scaleImage.saveImage(img,savePath, IMGMAXWIDTH);
				scaleImage.saveSmallImage(img,ToolsUtils.getApachePhysicalURL().get("apacheClippicPath")+"/"+smallWebPath,SMALLIMGMAXWIDTH,SMALLIMGMAXHEIGHT);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ClipeventPic clipeventPic=new ClipeventPic();
		clipeventPic.setEventid(0);
		clipeventPic.setPicurl(webPath);
		clipeventPicDAO.save(clipeventPic);
		return clipeventPic;
	}
	
	public void deleteUploadClippic(int picid) {
		ClipeventPic clipeventPic=clipeventPicDAO.findById(picid);
		if(clipeventPic!=null){
			String savePath=ToolsUtils.getApachePhysicalURL().get("apacheClippicPath")+"/"+clipeventPic.getPicurl();
			File picfile=new File(savePath);
			if(picfile.exists())
				picfile.delete();
			clipeventPicDAO.delete(clipeventPic);
		}
	}

	public void deleteTempClippic(ClipeventPic clipeventPic){
		String savePath=ToolsUtils.getApachePhysicalURL().get("apacheClippicPath")+"/"+clipeventPic.getPicurl();
		File picfile=new File(savePath);
		if(picfile.exists())
			picfile.delete();
		this.clipeventPicDAO.delete(clipeventPic);
	}
	
	public void updateClipeventpic(int eventid, int picid) {
		ClipeventPic clipeventPic=clipeventPicDAO.findById(picid);
		clipeventPic.setEventid(eventid);
		this.clipeventPicDAO.attachDirty(clipeventPic);
	}
	
	public String getClipeventPic(int eventid) {
		String query="from ClipeventPic pic where pic.eventid=?";
		List list=this.clipeventPicDAO.findByQuery(query,new Object[]{eventid});
		if(list!=null&&list.size()>0){
			ClipeventPic pic=(ClipeventPic) list.get(0);
			return ToolsUtils.getWebHeadUrl(null,ToolsUtils.WEBHEADURLPORT)+ToolsUtils.getApachePhysicalURL().get("webClippicPath")+"/"+pic.getPicurl();
		}else
			return null;
	}
}
